# chargamestate.rb
# created by: Dan Alexander
# date: 3/6/2011
# license: New BSD License
#
# CharacterSelectGameState
# The character selection game state, where players choose who
# to play as.

include Rubygame

class CharacterSelectState < GameState

	def initialize(scr)
		super(scr) #GameState parent class initialize
		
		@selected_character = 0
		#set up the fonts
		TTF.setup
		@fontTitle = TTF.new "resources/font.ttf", 28
		@fontItem = TTF.new "resources/font.ttf", 18
		
		#colors
		@colorTitle = [255,255,0]
		@colorMenu = [140,140,140]
		
		@text_warrior = "The warrior is the melee fighter, slashing at enemies within range."
		@text_mage = "The mage is the ranged fighter, hurling magic at enemies at a distance."
		
		@page = "character"
		@name = ""
		
		@name_keys = Array["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
	end
	
	def update
		$event_queue.each do |event|
			case event
				when Events::KeyReleased
					key = event.key
					if key == :escape
						$currentstate = "mainmenu"
					elsif key == :left && @page == "character"
						@selected_character -= 1
						if @selected_character < 0 then @selected_character = 1 end
					elsif key == :right && @page == "character"
						@selected_character += 1
						if @selected_character > 1 then @selected_character = 0 end
					elsif key == :return
						if @page == "character"
							@page = "name"
						else
							if @name.length > 0
								@selected_character == 0 ? chr = "warrior" : chr = "mage"
								$gamestates["ingame"].send_login(@name,chr)
								while $gamestates["ingame"].got_response == false do
									#sit here
									#if $debug then puts "waiting for response..." end
									sleep(0.01)
								end
								result = $gamestates["ingame"].loggedin
								if $debug then puts result end
								if result == true
									if $debug then puts "login was successful!" end
									$player_name = @name
									$player_image = chr
									$player_stats = Hash.new
									$currentstate = "ingame"
								else
									if $debug then puts "login failed" end
									$gamestates["message"].show "Login failed. This might be because the player name is already in use.", "Login Failed"
								end
							end
						end
					elsif key == :backspace
						if @name.length > 0
							@name = @name[0,@name.length-1]
						end
					else
						if @page == "name"
							k = key.to_s()
							if @name_keys.include?(k) == true then @name += k end
						end
					end				
					
				when Events::QuitRequested
					$should_run = false
			end
		end
  
	end
	
	def draw
		#fill the screen with a background color
		@screen.fill([0,0,0],[0,0,@screen.w(),@screen.h()])
		
		#pick which stuff to draw based on which character class is
		#selected.
		if @selected_character == 0
			bmp = $resman.get_tile("warrior").zoom_to 128,128,false
			text = "Warrior"
			text2 = @text_warrior
		elsif @selected_character == 1
			bmp = $resman.get_tile("mage").zoom_to 128,128,false
			text = "Mage"
			text2 = @text_mage
		end
		
		#draw the class title
		bmp.blit @screen, [@screen.width / 2 - bmp.w / 2,10,128,128]
		if @page == "character"
			text_surface = @fontTitle.render_utf8 text, true, @colorTitle
			rt = text_surface.make_rect
			rt.topleft = [@screen.width / 2 - rt.width / 2, 150]
			text_surface.blit @screen, rt
			
			#draw the class information text
			max_width = @screen.width / @fontItem.size_text("W")[0] #how many characters across
			line = text2
			yy = 200
			while line.length > max_width do
				line2 = line[0,max_width]
				line = line[max_width,line.length-max_width]
				
				text_surface = @fontItem.render_utf8 line2, true, @colorMenu
				rt = text_surface.make_rect
				rt.topleft = [@screen.width / 2 - rt.width / 2, yy]
				text_surface.blit @screen, rt
				
				yy += @fontItem.size_text("W")[1]
			end
			
			if line.length > 0
				text_surface = @fontItem.render_utf8 line, true, @colorMenu
				rt = text_surface.make_rect
				rt.topleft = [@screen.width / 2 - rt.width / 2, yy]
				text_surface.blit @screen, rt
			end
		else
			text_surface = @fontTitle.render_utf8 "Name:", true, @colorTitle
			rt = text_surface.make_rect
			rt.topleft = [@screen.width / 2 - rt.width / 2, 150]
			text_surface.blit @screen, rt
		
			if @name != ""
				text_surface = @fontItem.render_utf8 @name, true, @colorTitle
				rt = text_surface.make_rect
				rt.topleft = [@screen.width / 2 - rt.width / 2, 200]
				text_surface.blit @screen, rt
			end
		end
	end
	
end